﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using MY.WCF.Contracts;
using System.ServiceModel.Description;

namespace MY.WCF.Hosting
{
    public partial class CalculatorHost : ServiceBase
    {
        ServiceHost host;
        public CalculatorHost()
        {
            InitializeComponent();
            host = new ServiceHost(typeof(MY.WCF.Service.CalculatorService));//创建宿主
        }

        protected override void OnStart(string[] args)
        {
            Uri address = new Uri("http://localhost:9000/calculatorservice/metadata");
            WSHttpBinding binding = new WSHttpBinding();
            Type contract = typeof(ICalculator);

            //增加端点
            host.AddServiceEndpoint(contract, binding, address);

            //获取宿主的行为列表，如果没有服务原数据交换的行为，实例化添加服务原数据交换行为
            if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
            {
                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                behavior.HttpGetEnabled = true;
                behavior.HttpGetUrl = new Uri("http://localhost:9000/calculatorservice/metadata");//设置Http方式
                host.Description.Behaviors.Add(behavior);
            }

            //打开宿主
            host.Open();
        }

        protected override void OnStop()
        {
            host.Close();
        }
    }
}
